const SERVICE = require("../../lib/js/service.js");
const Golbal = require("../../lib/js/global.js");

Page({
  data:{
    publictiyImg: SERVICE.imgUrl + 'wuliu/879079329209516393.png',
    showPupop: false,    //显示城市 
    showPupop_2: false,       
    pickerVal: [0, 0],    //picker下标
    transifromGrid_status: false,   //转换状态
    start_val: null,    //出发点
    destination_val: null,    //目的地
    vehicleModel: ["轿车","越野车（SUV）","商务车（MPV）","其他"],   //获取车型
    vehicleModel_val: "",
  },

  onLoad(){
    let _this = this,
      td = this.data, 
      now = new Date(),
      year = now.getFullYear(),
      month = now.getMonth(),
      date = now.getDate();
    this.setData({
      year,
      month,
      date
    });
    
    // 获取城市
    wx.showNavigationBarLoading();
    SERVICE.POST(SERVICE.URL.getCity,{},(status,res)=>{
      wx.hideNavigationBarLoading();
      console.log("获取城市:",res);
      if(res.data.status == "success"){
        this.setData({
          city: res.data.addredss
        });
      }else{
        wx.showToast({
          title: "获取信息失败",
          icon: "none",
          mask: true
        });
      }
      
    });

  },

  /*分享*/ 
  onShareAppMessage(){
    
  },

  /*获取出发点*/ 
  getStart(e){
    let td = this.data;
    if (td.city){
      this.setData({
        showPupop: true,
        showPupop_2: false
      });
    }
  },

  /*获取目的地*/
  getDestination(e){
    let td = this.data;
    if(td.city){
      this.setData({
        showPupop: false,
        showPupop_2: true
      });
    }    
  },
  
  /*获取日期*/ 
  getDate(e){
    console.log(e);
    let td = this.data;
    this.setData({
      Date: e.detail.value
    });
  },

  /*获取车型*/ 
  getVehicleModel(e){
    let td = this.data;
    this.setData({
      vehicleModel_val: td.vehicleModel[e.detail.value]
    });
  },
  
  /*城市*/
  pickerChange(e) {
    let td = this.data;
    if (e.detail.value[0] != td.pickerVal[0]) {
      td.pickerVal[1] = 0;
      td.pickerVal[0] = e.detail.value[0];
    } else {
      td.pickerVal[1] = e.detail.value[1];
    }

    this.setData({
      pickerVal: td.pickerVal
    });
  },

  /*取消*/
  pupopCancel() {
    this.setData({
      showPupop: false,
      showPupop_2: false
    });
  },

  /*确定*/
  pupopConfirm() {
    let td = this.data, city = td.city, pickerVal = td.pickerVal;
    if (td.showPupop){
      if (city[pickerVal[0]].city[pickerVal[1]] != td.destination_val){
       this.setData({
         showPupop: false,
         showPupop_2: false,
         start_val: city[pickerVal[0]].city[pickerVal[1]]
       });
     }else{
        wx.showToast({
          title: "不能选择相同的",
          icon: "none",
          mask: true,
          duration: 1500
        });
     }
    } else if (td.showPupop_2){
      if (city[pickerVal[0]].city[pickerVal[1]] != td.start_val){
        this.setData({
          showPupop: false,
          showPupop_2: false,
          destination_val: city[pickerVal[0]].city[pickerVal[1]]
        });
      }else{
        wx.showToast({
          title: "不能选择相同的",
          icon: "none",
          mask: true,
          duration: 1500
        });
      }
      
    }
  },

  /*转换*/ 
  transiformGrid(){
    let td = this.data,
    originalStart_val = td.start_val,
    originalDestination_val = td.destination_val;

    if (td.start_val && td.destination_val){
        td.start_val = originalDestination_val;
        td.destination_val = originalStart_val;
      this.setData({
        start_val: td.start_val,
        destination_val: td.destination_val,
        transifromGrid_status: !td.transifromGrid_status
      });
    }
  },

  /*电话*/
  mobile(e){
    let get_mobile = e.detail.value;
    if (get_mobile.length > 0 && Golbal.checkPhone(get_mobile) == false){
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      });
    }
  },

  /*马上报价*/
  formSubmit(e){
    let _this = this, form_obj = e.detail.value;
    if (!form_obj.start){
      wx.showToast({
        title: "请选择出发点",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.destination){
      wx.showToast({
        title: "请选择目的地",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.date) {
      wx.showToast({
        title: "请选择发车时间",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.num) {
      wx.showToast({
        title: "请输入托运台数",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.num) {
      wx.showToast({
        title: "请输入托运台数",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.vehiclemodel) {
      wx.showToast({
        title: "请选择托运车型",
        icon: "none",
        mask: true
      });
    }else if (!form_obj.name) {
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.mobile) {
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        mask: true
      });
    } else if (Golbal.checkPhone(form_obj.mobile) == false) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
        mask: true
      });
    }else{
      wx.showLoading({
        title: "报价中...",
        mask: true
      });
      SERVICE.POST(SERVICE.URL.logistics,{
        origin: form_obj.start,
        destination: form_obj.destination,
        start_time: form_obj.date,
        num: form_obj.num,
        branch: form_obj.vehiclemodel,
        name: form_obj.name,
        phone: form_obj.mobile,
        invoice: form_obj.invoice ? 1 : 0,
        up_door: form_obj.uodoor ? 1 : 0,
        send_door: form_obj.senddoor ? 1 : 0,
        append_insurance: form_obj.insurance ? 1 : 0
      },(status,res)=>{
        wx.hideLoading();
        if(res.data.status === "success"){
          wx.showModal({
            title: "提示",
            content: "您好，已收到您的托运订单，请保持电话畅通，客服即将与您联系！",
            cancelText: "重新报价",
            confirmText: "关闭",
            success: function(res){
              if(res.confirm){
                wx.navigateBack();
              }else if(res.cancel){
                _this.setData({
                  start_val: null,
                  destination_val: null,
                  year: null,
                  month: null,
                  date: null,
                  Date: null,
                  name: null,
                  mobile: null,
                  num: null,
                  vehicleModel_val: null,
                  Switch: false
                });
              }
            }
          })
         
        }else{
          wx.showToast({
            title: "报价失败",
            icon: "none",
            mask: true
          });
        }
      });
    }
  } 

});